package com.yz.group1.group_1.controller;
import com.yz.group1.group_1.entity.DoctorVisit;
import com.yz.group1.group_1.service.DoctorVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/visit_manage")
public class DoctorVisitController {
    @Autowired
    private DoctorVisitService doctorVisitService;


    @GetMapping
    public List<DoctorVisit> doctorVisitList() {
        return doctorVisitService.listAllDoctorVisits();
    }
    @GetMapping("/today/{doctorId}")
    public List<DoctorVisit> today(@PathVariable Long doctorId) {
        return doctorVisitService.todayQueue(doctorId);
    }

    // 修改就诊状态
    @PutMapping("/status/{id}")
    public boolean status(@PathVariable Long id, @RequestParam Integer status) {
        return doctorVisitService.lambdaUpdate()
                .eq(DoctorVisit::getId, id)
                .set(DoctorVisit::getStatus, status)
                .update();
    }

    // 填写诊断
    @PutMapping("/diagnosis/{id}")
    public boolean diagnosis(@PathVariable Long id,
                             @RequestParam String diagnosis,
                             @RequestParam String prescription) {
        return doctorVisitService.lambdaUpdate()
                .eq(DoctorVisit::getId, id)
                .set(DoctorVisit::getDiagnosis, diagnosis)
                .set(DoctorVisit::getPrescription, prescription)
                .update();
    }

    // 提交住院申请
    @PutMapping("/hospital/{id}")
    public boolean hospital(@PathVariable Long id) {
        return doctorVisitService.lambdaUpdate()
                .eq(DoctorVisit::getId, id)
                .set(DoctorVisit::getHospitalFlag, 1)
                .update();
    }

    // 全局配置（推荐）：新建配置类
    @Configuration
    public class CorsConfig implements WebMvcConfigurer {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**") // 允许所有接口
                    .allowedOrigins("http://127.0.0.1:5500") // 前端运行地址
                    .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许请求方法
                    .allowedHeaders("*") // 允许所有请求头
                    .allowCredentials(true); // 允许携带 Cookie（如需登录态）
        }
    }
}
